import SwiftUI

struct CellView: View {
    let cell: Cell
    let size: CGFloat
    let onTap: () -> Void
    let onLongPress: () -> Void
    
    var body: some View {
        ZStack {
            Rectangle()
                .fill(cellBackgroundColor)
                .frame(width: size, height: size)
            
            if cell.state == .revealed {
                if cell.isMine {
                    Image(systemName: "circle.fill")
                        .font(.system(size: size * 0.5))
                        .foregroundColor(.black)
                } else if cell.neighborMines > 0 {
                    Text("\(cell.neighborMines)")
                        .font(.system(size: size * 0.6, weight: .bold))
                        .foregroundColor(numberColor)
                }
            }
            
            if cell.state == .flagged {
                Image(systemName: "flag.fill")
                    .foregroundColor(cell.isMine ? .red : .gray)
                    .font(.system(size: size * 0.7))
                    .shadow(color: .black.opacity(0.2), radius: 2, x: 1, y: 1)
                    .overlay(
                        RoundedRectangle(cornerRadius: 4)
                            .stroke(cell.isMine ? Color.red : Color.gray, lineWidth: 1)
                    )
            }
            
            if cell.state == .questioned {
                Text("?")
                    .font(.system(size: size * 0.6, weight: .bold))
                    .foregroundColor(.gray)
            }
        }
        .contentShape(Rectangle())
        .onTapGesture {
            onTap()
        }
        .gesture(
            LongPressGesture(minimumDuration: 0.3)
                .onEnded { _ in
                    onLongPress()
                }
        )
        .animation(.easeInOut(duration: 0.1), value: cell.state)
        .scaleEffect(cell.state == .revealed ? 1.0 : 0.95)
        .shadow(color: .gray.opacity(0.3), radius: 2, x: 1, y: 1)
    }
    
    private var cellBackgroundColor: Color {
        cell.state == .revealed ? Color(white: 0.9) : Color(red: 0.8, green: 0.85, blue: 1.0)
    }
    
    private var numberColor: Color {
        switch cell.neighborMines {
        case 1: return .blue
        case 2: return .green
        case 3: return .red
        case 4: return .purple
        default: return .black
        }
    }
} 